var JcropOption = {
    actual:false,
    activate:true,
    ratio:2/1,
    croper:'novedad.croper.php'
}

var imagesInOrder = false;

$(document).ready(function () {
    if($('#sortableImages li').length) {
        $('.fileinput-button').hide();
    } else {
        $('.fileinput-button').show();
    }
    $('#formNovedad').submit(function () {
        pasarData();
        $('#loaderModal').modal('show')
    });
    fileuploaderInit();
    onfileuploaddone();
    dropzone();
})

function onfileuploaddone () {
    $('.recortarImagen, .borrarImagen').unbind('click')
    $('.recortarImagen').click(recortarImagen)
    $('.borrarError').click(borrarError)
    $('.borrarImagen').click(borrarImagen)
    if($('#sortableImages li').length) {
        $('.fileinput-button').hide();
    } else {
        $('.fileinput-button').show();
    }
}

function fileuploaderInit () {
    $('#formNovedad').fileupload({
        url: BASE_URL+'admin/php/uploaders/novedad.uploader.php',
        autoUpload:true,
        fileInput:$('#portada')
    })
    .bind('fileuploaddone', function (e, data) {
        $('#hiddenData').append('<input type="hidden" name="newImages[]" id="'+data.result.files[0].nombre+'" value="5" />')
        setTimeout(
            function () {
                $('.preview > img.recortable').imagesFrame(120, 60, true)
                onfileuploaddone();
            }, 
            300
        )
    })
    .bind('fileuploadfail', function (e, data){
        setTimeout(
            function () {
                $('.preview > img.recortable').imagesFrame(60, 60, true)
                onfileuploaddone();
            }, 
            300
        )
    })
}

function recortarImagen () {
    crearJcrop($(this).parents('li').attr('id'))
    $('#coordenadas .nombre').val($(this).parents('li').find('.name').text())
}

function borrarImagen () {
    var data = {
        src:$(this).data('src'),
        carpeta:$(this).data('carpeta'),
        id:($(this).data('id'))?$(this).data('id'):0
    }
    $('#'+$(this).data('src').replace(/\.(jpg|png|gif)$/, '')).remove();
    $.ajax({
        type:'POST',
        url:BASE_URL+'admin/php/controllers/borrarImagenNovedad.controller.php',
        data: data
    })
    $(this).parents('li').remove();
    if($('#sortableImages li').length) {
        $('.fileinput-button').hide();
    } else {
        $('.fileinput-button').show();
    }
}

function borrarError () {
    $(this).parents('li').remove();
    if($('#sortableImages li').length) {
        $('.fileinput-button').hide();
    } else {
        $('.fileinput-button').show();
    }
}

function pasarData () {
    var data = '', id;
    $('#sortableImages li').each(function (i) {
        if (id = $(this).data('id')) {
            data += '&orden[]='+i;
            data += '&id[]='+id;
        } else {
            $('#hiddenData #'+$(this).data('nombre')).val($(this).data('nombre')+','+i+','+$(this).data('extension'))
        }
    }) 
}

function dropzone () {
    $(document).bind('dragover', function (e) {
        var dropZone = $('#dropzone'),
            timeout = window.dropZoneTimeout;
        if (!timeout) {
            dropZone.addClass('in');
        } else {
            clearTimeout(timeout);
        }
        if (e.target === dropZone[0]) {
            dropZone.addClass('hover');
        } else {
            dropZone.removeClass('hover');
        }
        window.dropZoneTimeout = setTimeout(function () {
            window.dropZoneTimeout = null;
            dropZone.removeClass('in hover');
        }, 100);
    });
}